package giab.weixin.web.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

public class EncryptSHA {

	public static String getSHA1ToHex(String data) {
		byte[] bytes = getSHA1ToByte(data);
		if (bytes != null) {
			return Hex.encodeHexString(bytes);
		} else {
			return null;
		}
	}

	public static String getSHA1ToBase64(String data) {
		byte[] bytes = getSHA1ToByte(data);
		if (bytes != null) {
			return Base64.encodeBase64String(bytes);
		} else {
			return null;
		}
	}

	public static byte[] getSHA1ToByte(String data) {
		byte[] shaByte = null;
		try {
			MessageDigest md = MessageDigest.getInstance("SHA1");
			shaByte = md.digest(data.getBytes());
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return shaByte;
	}

	public static void main(String args[]) {

		String sing = "a8b7c41e5ce9f6533e29b8b04f55a2c488e56fe2";
		String base = getSHA1ToHex("102056947613661871391366279446");
		if (sing.equals(base)) {
			System.err.println("OK");
		}
	}

}
